<?php
//日本語
class Message extends AppModel {

	var $name = 'Message';
	var $recursive = 0;
	var $belongsTo = array(
		'UserFrom' => array(
			'className' => 'User',
			'foreignKey' => 'userfrom',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'UserTo' => array(
			'className' => 'User',
			'foreignKey' => 'userto',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
	);
	//バリデート
	var $validate = array(
		'status' => array(
			'rule' => array('isInArray', array('active', 'disable')),
		),
		'receiver' => array(
			'rule' => array('isInArray', array('yet', 'read', 'disable')),
		),
		'sender' => array(
			'rule' => array('isInArray', array('already', 'disable')),
		),
		'userfrom' => array(
			'rule' => array('isPlusNumber'),
		),
		'userto' => array(
			'rule' => array('isPlusNumber'),
		),
		'ip' => array(
			'rule' =>array('ip'),
		),
		'title' => array(
			VALID_NOT_EMPTY,
			'length' => array(
				'rule' =>array('between', 1, 100),
				'message' => 'タイトルは1-100文字までです',
			),
			'kaigyo' => array(
				'rule' => array('kaigyoLessThanN', 0),
				'message' => 'タイトルは改行できません',
			),
		),
		'content' => array(
			VALID_NOT_EMPTY,
			'length' => array(
				'rule' =>array('between', 1, 2000),
				'message' => '内容は1-2000文字までです',
			),
			'kaigyo' => array(
				'rule' => array('kaigyoLessThanN', 19),
				'message' => '内容は20行までです',
			),
		),
	);
	
	function post($from, $to, $title, $content)
	{
		$data = array();
		$data['userfrom'] = $from;
		$data['userto'] = $to;
		$data['title'] = $title;
		$data['content'] = $content;
		$data['status'] = 'active';
		$data['receiver'] = 'yet';
		$data['sender'] = 'already';
		$data['ip'] = $this->RHgetClientIP();
		
		return $this->save(array($this->name => $data));

	}

}
?>